#ifndef _Ape__CollectionsHelpers
#define _Ape__CollectionsHelpers

#include <stdlib.h>


#define _APE_OPT_ARRAY_MIN_BYTES_ALLOCATE 4

namespace Ape {
	namespace Collections {
	
		/**
			TypedHelper - type-aware helper for collections
			@author Roman Gafiyatullin
		*/
		template<typename E_t>
		struct TypedHelper {
			/**
				Creates a copy of item on the heap
				@param item statically allocated variable
				@return a copy of item allocated on the heap
			*/
			static void* ToHeap (const E_t& item) {
				return reinterpret_cast<void*> (new E_t (item) );
			}
			/**
				Creates a copy of value under ptr on the stack
				@param ptr pointer to the value to copy
				@return a copy of item allocated on the stack
			*/
			static E_t ToStack (void* ptr) {
				return E_t (*reinterpret_cast<E_t*> (ptr) );
			}
			/**
				Creates a reference to the value under pointer
				@param ptr pointer to the value to reference to
				@return a reference to the item passed in ptr
			*/
			static E_t& RefToStack (void* ptr) {
				E_t* pItem = reinterpret_cast<E_t*> (ptr);
				return *pItem;
			}
			/**
				Frees the memory under ptr
				@param ptr pointer to the value
			*/
			static void Free (void* ptr) {
				delete reinterpret_cast<E_t*> (ptr);
			}
		};
		
		struct Helper {
			static size_t CalcAllocateSize (size_t iAtLeast, int sizeOfElement);
		};
		
// defs

#define foreach(i, Container_t, container) \
	for ( \
			Container_t::Iterator_t i(container); \
			i.Continue(); \
			i++ \
		) \
		 
	}}
	
#endif // _Ape__CollectionsHelpers
	
